package com.bobi.tfca.ui.article

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.PageElementIdConstant
import com.bobi.tfca.databinding.FragmentArticleDetailBinding
import com.bobi.tfca.network.client.AppCache
import com.bobi.tfca.ui.adapter.ShowParagraphAdapter

class ShowArticleFragment : BaseFragment<FragmentArticleDetailBinding, ArticleFragmentVM>(
    FragmentArticleDetailBinding::inflate,
    ArticleFragmentVM::class.java,
    true
) {
    // 会员RV
    private lateinit var paragraphRV: RecyclerView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentArticleDetailBinding,
        viewModel: ArticleFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化topPanel
        setTopPanelStyle(binding)

        // 初始化RV
        initRecyclerView(binding)

        // 监听viewmodel
        publicViewModel!!.article.observe(this@ShowArticleFragment) {
            binding.tvTitle.text = it.title
            binding.tvAuthor.text = it.authorName
            binding.tvDetailsTime.text = it.releaseTime
            binding.tvLikes.text = it.likes.toString()
            binding.ivLikes.isSelected = it.isLiked
            val adapter = paragraphRV.adapter as ShowParagraphAdapter
            if (it.paragraphList != null) {
                adapter.setItemList(it.paragraphList)
                adapter.notifyDataSetChanged()
            }
        }

        // 设置菜单为监听的
        publicViewModel!!.apply {
            visiblePageElements.observe(this@ShowArticleFragment) {
                binding.tvDelete.visibility = getElementVisibility(PageElementIdConstant.FRAGMENT_ARTICLE_DETAIL_TV_DELETE)
            }
        }

        //  设置点赞事件
        binding.ivLikes.setOnClickListener {
            val loginStatus = AppCache.checkLoginStatus()
            if (loginStatus) {
                val isLiked = binding.ivLikes.isSelected
                viewModel!!.likeArticle(publicViewModel!!, !isLiked, this)
            } else {
                Toast.makeText(requireActivity(), "请登录或注册", Toast.LENGTH_SHORT).show()
            }
        }

        // 设置删除事件
        binding.tvDelete.setOnClickListener {
            // 创建确认框
            val builder = AlertDialog.Builder(requireContext())
            builder.setTitle("删除")
            builder.setMessage("你确认删除文章吗")

            // 设置确认按钮和点击事件
            builder.setPositiveButton("确认") { dialog, which ->
                viewModel!!.deleteArticle(publicViewModel!!, this)
            }

            // 设置取消按钮和点击事件
            builder.setNegativeButton("取消") { dialog, which ->
            }

            // 创建并显示确认框
            val dialog: AlertDialog = builder.create()
            dialog.show()
        }

        // 请求标题，作者，时间
        getArticleDetails(viewModel!!)
    }


    private fun getArticleDetails(viewModel: ArticleFragmentVM) {
        // 请求标题，作者，时间
        viewModel.getArticleDetails(publicViewModel!!, this)
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(
        binding: FragmentArticleDetailBinding
    ) {
        val topPanel = binding.topPanel
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener {
            // 返回重置article
            publicViewModel!!.resetArticle()
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        topPanel.rightLayout.visibility = View.GONE
        val title = topPanel.titleText
        title.text = "文章"
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentArticleDetailBinding
    ) {
        // 初始化RV
        paragraphRV = binding.rvParagraph
        // 设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        paragraphRV.layoutManager = linearLayoutManager
        // 添加分隔线
        paragraphRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        // 绑定adapter
        val showParagraphAdapter = ShowParagraphAdapter(this, arrayListOf())

        paragraphRV.adapter = showParagraphAdapter
    }

}